package com.facefun.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.facefun.Enum.StateCode;
import com.facefun.co.ChaptorCo;
import com.facefun.domain.Cartoon;
import com.facefun.domain.Store;
import com.facefun.domain.User;
import com.facefun.service.CartoonService;
import com.facefun.service.ChaptorService;
import com.facefun.service.StoreService;
import com.facefun.service.TypeService;
import com.facefun.service.UserService;
import com.facefun.vo.CartoonSearchVo;

@Controller
public class FrontController {

	@Autowired
	private CartoonService cartoonService;
	
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private ChaptorService chaptorService;
	
	@Autowired
	private UserService userSerivce;
	
	@Autowired
	private StoreService storeService;
	
	@PostMapping("/search")
	public String search(String searchword,Model model){
		CartoonSearchVo vo=new CartoonSearchVo(searchword);
		model.addAttribute("searchword", searchword);
		model.addAttribute("list", cartoonService.getSearchList(vo));
		
		return "front/search";
	}
	
	@PostMapping("/category")
	public @ResponseBody List<Cartoon> getSearchList(CartoonSearchVo vo){
		return cartoonService.getSearchList(vo);
	}
	
	@GetMapping("/reader/{ctId}/{cpId}")
	public String chaptorInfo(@PathVariable("ctId")Integer ctId,
			@PathVariable("cpId") Integer cpId,Model model){
		ChaptorCo c=chaptorService.getChaptorCoByKey(ctId, cpId);
		if(c==null){
			cpId--;
			return "redirect:/reader/cuigeng/"+ctId+"/"+cpId;
		}
		model.addAttribute("cp",c );
		return "front/itemsee";
	} 
	
	@GetMapping("/reader/cuigeng/{ctId}/{cpId}")
	public String cuigeng(@PathVariable("ctId")Integer ctId,
			@PathVariable("cpId") Integer cpId,Model model){
		model.addAttribute("cp",chaptorService.getChaptorCoByKey(ctId, cpId));
		return "front/cuigeng";
	}
	
	@GetMapping("/update")
	public String update(Model model){
		CartoonSearchVo vo=new CartoonSearchVo(false);
		model.addAttribute("list", cartoonService.getSearchList(vo));
		return "front/update";
	}
	
	
	@GetMapping("/over")
	public String over(Model model){
		CartoonSearchVo vo=new CartoonSearchVo(true);
		model.addAttribute("list", cartoonService.getSearchList(vo));
		return "front/over";
	}
	
	
	@GetMapping("/category")
	public String category(CartoonSearchVo vo,Model model){
		model.addAttribute("types",typeService.selectAll());
		model.addAttribute("list", cartoonService.getSearchList(vo));
		return "front/category";
	}
	
	@GetMapping("/cartoon/{id}")
	public String cartoonInfo(@PathVariable("id")Integer key
			,@SessionAttribute(name="user",required=false)User user,Model model){
		model.addAttribute("c", cartoonService.getCartoonCoByKey(key));
		model.addAttribute("ranklist", cartoonService.getRankList());
		Integer uId=user!=null?user.getId():null;
		model.addAttribute("isStore", storeService.isStore(new Store(key,uId)));
		return "front/iteminfo";
	}
	
	@GetMapping("/index")
	public String index(Model model){
		model.addAttribute("types",typeService.selectAll());
		model.addAttribute("ranklist", cartoonService.getRankList());
		return "front/index";
	}
	
	@PostMapping("/user/login")
	public @ResponseBody  StateCode userLogin(User user,HttpSession session){
		User u=userSerivce.selectOne(user);
		if(u !=null){
			session.setAttribute("user", u);
			return StateCode.SUCCESS;
		}
		return StateCode.NOTEXISIT;
	}
	
	@GetMapping("/user/regist")
	public String userRegistPage(Model model){
		model.addAttribute("user", new User());
		model.addAttribute("types", typeService.selectAll());
		return "front/login";
	}
	
	@GetMapping("/user/eidtor")
	public String userEidtorPage(Model model){
		model.addAttribute("types", typeService.selectAll());
		return "front/login";
	}
	
	@PostMapping("/user")
	public String userRegist(@RequestParam("head")MultipartFile file,User user,HttpServletRequest request){
		try {
			request.getSession().setAttribute("user", userSerivce.addUser(user, file, request));
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			return "front/login";
		}
		
		return "redirect:/index";
	}
	
	@PutMapping("/user")
	public String userPut(@RequestParam("head")MultipartFile file,User user,HttpServletRequest request){
	
		request.getSession().setAttribute("user", userSerivce.updateUser(user, file, request));
		
		return "redirect:/index";
	}
	
	@GetMapping("/user/exit")
	public String userExit(HttpSession session){
		session.removeAttribute("user");
		return "redirect:/index";
	}
	
	
	@PostMapping("/reader/store")
	public @ResponseBody StateCode userStore(@RequestParam("ctId")Integer ctId,
			@SessionAttribute(name="user",required=false)User user){
		if(user!=null)
			return storeService.addStore(new Store(ctId, user.getId()));
		return StateCode.NOTEXISIT;
	}
	
	@DeleteMapping("/reader/store")
	public @ResponseBody StateCode userStoreDelete(@RequestParam("ctId")Integer ctId,
			@SessionAttribute(name="user",required=false)User user){
		if(user!=null)
			return storeService.removeStore(new Store(ctId, user.getId()));
		return StateCode.NOTEXISIT;
	}
	
	@GetMapping("/store")
	public String userStore(@SessionAttribute(name="user")User user,Model model){
		model.addAttribute("list", storeService.getStoreList(user.getId()));
		return "front/store";
	}
	
	@PostMapping("/store/list")
	public @ResponseBody Map<String, Object> userStoreList(@SessionAttribute(name="user",required=false)User user,Model model){
		Integer uId=user!=null?user.getId():0;
		return storeService.getStoreList(uId,0,2);
	}
	
}
